<?php
//自定义变量数据处理
class fragmentModel extends commonModel {

	public function __construct()
    {
        parent::__construct();
    }

    //获取自定义变量列表
    public function fragment_list()
    {
        return $this->model->table('fragment')->order('id asc')->select();
    }

    //获取自定义变量内容
    public function info($id)
    {
        return $this->model->table('fragment')->where('id='.$id)->find();
    }

    //添加自定义变量内容
    public function add($data)
    {
        return $this->model->table('fragment')->data($data)->insert();
    }

    //编辑自定义变量内容
    public function edit($data)
    {
        $condition['id']=intval($data['id']);
        return $this->model->table('fragment')->data($data)->where($condition)->update(); 
    }

    //删除自定义变量内容
    public function del($id)
    {
        return $this->model->table('fragment')->where('id='.intval($id))->delete(); 
    }
    
     //获取字段类型名称
    public function field_type($id=null,$name=false)
    {
        $list=array(
            1=> array(
                'name'=>'文本框'
                ),
            2=> array(
                'name'=>'多行文本'
                ),
            3=> array(
                'name'=>'编辑器'
                ),
            4=> array(
                'name'=>'文件上传'
                ),
            10=> array(
                'name'=>'单图片上传'
                ),
            5=> array(
                'name'=>'组图上传'
                ),
            7=> array(
                'name'=>'日期和时间'
                ),
            8=> array(
                'name'=>'单选开关'
                ),
        );
        return $list;
    }

    //获取字段HTML
    public function get_field_html($info,$data=null){
        $info['default']=html_out($info['default']);
        if($data=='0' || !empty($data)){
            $info['default']=$data;
        }
        $html='';
        switch ($info['type']) {
            case '1':
                $html.='<input name="'.$info['field'].'" type="text" class="text_value" id="'.$info['field'].'" value="'.$info['default'].'" />';
                break;
            case '2':
                $html.='<textarea name="'.$info['field'].'" class="text_textarea" id="'.$info['field'].'" >'.$info['default'].'</textarea>';
                break;
            case '3':
                $html.='
                    <textarea name="'.$info['field'].'" style="width:100%; height:350px;" id="'.$info['field'].'">'.$info['default'].'</textarea>
                    '.module('editor')->get_editor_upload($info['field'].'_upload','editor_'.$info['field']).'
                    <input type="button" id="'.$info['field'].'_upload" class="button_small" style="margin-top:10px;" value="上传图片和文件到编辑器" />
                ';
                $html.=module('editor')->get_editor($info['field'],true);
                break;
            case '4':
                $html.='
                    <input name="'.$info['field'].'" type="text"  class="text_value"  style="width:200px; float:left"  id="'.$info['field'].'" value="'.$info['default'].'" />
                    &nbsp;&nbsp;<input type="button" id="'.$info['field'].'_botton" class="button_small" value="选择文件" />';
                $html.=module('editor')->get_file_upload($info['field'].'_botton',$info['field'],true);
                break;
            case '10':
                $html.='
                    <input name="'.$info['field'].'" type="text"  class="text_value"  style="width:200px; float:left"  id="'.$info['field'].'" value="'.$info['default'].'" />
                    &nbsp;&nbsp;<input type="button" id="'.$info['field'].'_botton" class="button_small" value="选择图片" />
                ';
                $html.=module('editor')->get_image_upload($info['field'].'_botton',$info['field'],true);
                break;
            case '5':
                $html.='
                    <input type="button" id="'.$info['field'].'_button" class="button_small" value="上传多图" />
                    <div class="fn_clear"></div>
                    <div class="images">
                    <ul id="'.$info['field'].'_list" class="images_list">';
                if(!empty($data)){
                    $info['default']=unserialize(html_out($info['default']));
                    if(!empty($info['default'])){
                        foreach ($info['default'] as $value) {
                        $html.="<li>
                                <div class='pic' id='images_button'>
                                <img src='".$value['url']."' width='125' height='105' />
                                <input  id='".$info['field']."[]' name='".$info['field']."[]' type='hidden' value='".$value['url']."' />
                                <input  id='".$info['field']."_original[]' name='".$info['field']."_original[]' type='hidden' value='".$value['original']."' />
                                </div>
                                <div class='title'>标题： <input name='".$info['field']."_title[]' type='text' id='".$info['field']."_title[]' value='".$value['title']."' /></div>
                                <div class='title'>排序： <input id='".$info['field']."_order[]' name='".$info['field']."_order[]' value='".$value['order']."' type='text' style='width:50px;' /> <a href='javascript:void(0);' onclick='$(this).parent().parent().remove()'>删除</a></div>
                            </li>";
                        }
                    }
                }
                $html.="</ul>
                    <div style='clear:both'></div>
                    </div>
                ";
                $html.=module('editor')->get_images_upload($info['field'],$ajax=true);
                break;
            case '7':
                $config=explode("\n", $info['config']);
                if($data && intval($info['default'])>0){
                    $info['default']=date('Y-m-d H:i:s',intval($info['default']));
                }else{
                    $info['default']=date('Y-m-d H:i:s',time());
                }
                $html.='<input name="'.$info['field'].'"  id="'.$info['field'].'" type="text" class="text_value" style="width:210px; float:left" value="'.$info['default'].'" />';
                $html.='<div id="'.$info['field'].'_button" class="time"></div>';
                $html.='<script>';
                $html.="$('#".$info['field']."_button').calendar({ id:'#".$info['field']."',format:'yyyy-MM-dd HH:mm:ss'});";
                $html.='</script>';
                break;
            case '8':
                $info['default'] = intval($info['default']);
                $html.='<label><input name="'.$info['field'].'" type="radio" value="1" ';
                if ($info['default']==1){
                    $html.='checked="checked" ';
                }
                $html.='/> 开启</label>';
                $html.='&nbsp;&nbsp;<label><input name="'.$info['field'].'" type="radio" value="0" ';
                if ($info['default']==0){
                    $html.='checked="checked" ';
                }
                $html.='/> 关闭</label>';
                break;
        }
        return $html;
    }



}